package com.example.ruleorcutepicture;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.icu.math.BigDecimal;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RulerView rulerView=new RulerView(this);
        setContentView(rulerView);
    }
}
class RulerView extends View {
    int densityDpi;
    float scaledDensity;
    float density;
    float xdpi;
    float ydpi;
    int width;
    int height;
    Display display;
    private Paint paint;//用于绘制线条
    private Paint textPaint;//用于绘制文字
    private int count = 10;
    private int length = count * 10;

    public RulerView(Context context) {
        super(context);
        densityDpi = context.getResources().getDisplayMetrics().densityDpi;
        scaledDensity=context.getResources().getDisplayMetrics().scaledDensity;
        density=context.getResources().getDisplayMetrics().density;
        xdpi = context.getResources().getDisplayMetrics().xdpi;
        ydpi =context.getResources().getDisplayMetrics().ydpi;//每英寸像素数
        width = context.getResources().getDisplayMetrics().widthPixels;
        height = context.getResources().getDisplayMetrics().heightPixels;//没有屁用的玩意

        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);//使用此样式绘制的几何图形和文本将被描边，尊重油漆上与描边相关的字段
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(1);
        paint.setColor(Color.GRAY);//设置填充模式，笔画粗细，笔画颜色

        textPaint = new Paint();
        textPaint.setStyle(Paint.Style.FILL);//使用此样式绘制的几何图形和文本将被填充，忽略油漆中所有与笔划相关的设置
        textPaint.setTextSize(30);
        textPaint.setColor(Color.GRAY);//设置填充模式，文字大小，笔画颜色
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();//得到事先设定的文本大小
        float fontHeight = fontMetrics.descent - fontMetrics.ascent;
        int width = getMeasuredWidth();//获取组件的宽度
        int height = getMeasuredHeight();//获取组件的高度

        /*float mm_xdpi=xdpi/inch_translate_into_mm(1);//一mm有多少像素*/

        BigDecimal bigDecimal = new BigDecimal(width - 2);
        float margin = (float) bigDecimal.multiply(new BigDecimal(0.1)).divide(new BigDecimal(2)).doubleValue();
        canvas.drawRect(1, 2, width - 1, height - 2, paint);

        BigDecimal mm=new BigDecimal(xdpi).divide(new BigDecimal(inch_translate_into_mm(1)));
        //计算出高度

        Path path = new Path();

        for (int i = 0; i <= length; i++) {
            float x = (float) (margin + mm.multiply(new BigDecimal(i)).doubleValue());
            path.moveTo(x, height - 2);
            x = (float) (margin + mm.multiply(new BigDecimal(i)).doubleValue());
            if (i == 0 || i % 10 == 0) {
                path.lineTo(x, (float) ((height) * 0.4));
                String units = i / 10 + "";
                if (i == 0) {
                    units += "cm";
                    //0刻度的时候，0应该在刻度的正上方
                    canvas.drawText(units, x - textPaint.measureText("0") / 2, (float) ((height) * 0.4) - fontHeight / 4, textPaint);
                } else {
                    float textWidth = textPaint.measureText(units);
                    canvas.drawText(units, x - textWidth / 2, (float) ((height) * 0.4) - fontHeight / 4, textPaint);
                }
            } else if (i % 5 == 0) {
                path.lineTo(x, (float) ((height) * 0.5));
            } else {
                path.lineTo(x, (float) ((height) * 0.7));
            }
        }
        canvas.drawPath(path, paint);
    }
    private static float inch_translate_into_mm(float inch)
    {
        return (float) (inch*25.4f);
    }
    private static  float mm_translate_into_inch(float mm)
    {
        return (float) (mm/25.4f);
    }
}
